<template>
    <div class="game-all-data-box" v-loading="loading">
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;margin-right:35px;">日期</span>
            <el-date-picker
                @change="clearGetList"
                v-model="date"
                type="daterange"
                style="width:345px"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>

            <span class="input-title">渠道</span>
            <el-select
                style="width:110px;"
                @change="clearGetList"
                v-model="source">
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="taptap" :value="0"></el-option>
                <el-option label="应用宝" :value="1"></el-option>
                <el-option label="客户" :value="2"></el-option>
                <el-option label="学习识别" :value="3"></el-option>
                <el-option label="人工" :value="4"></el-option>
                <el-option label="小米" :value="5"></el-option>
            </el-select>

            <el-button type="primary" class="btn" @click="clearGetList">搜索</el-button>
        </div>

        <div class="table-box">
            <el-table
                ref="table"
                :data="list"
                border
                @sort-change="sortChange"
                style="width: 100%">
                <el-table-column
                    width="110"
                    prop="createTime"
                    label="时间">
                    <template slot-scope="scope">
                        <span>{{scope.row.date | timeInit}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="downloadGameNum"
                    label="下载游戏数">
                    <template slot-scope="scope">
                        <span>{{scope.row.downloadGameNum || 0}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="gameDownloadNum"
                    label="下载量">
                    <template slot-scope="scope">
                        <div>{{scope.row.gameDownloadNum || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="startNum"
                    label="启动量">
                    <template slot-scope="scope">
                        <span>{{scope.row.startNum || '0'}}</span>
                        <span v-if="scope.row.startUserNum">（{{scope.row.startUserNum || '0'}}人）</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="startGameNum"
                    label="启动游戏数">
                    <template slot-scope="scope">
                        <span>{{scope.row.startGameNum || '0'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    width="100"
                    type="expand"
                    label="查看详情">
                    <template slot-scope="scope">
                        <gameAllDataExpand :time="scope.row.date | timeInit" :source="source"/>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
import gameAllDataExpand from './gameAllDataExpand' // 展开组建
export default {
    data () {
        return {
            loading: false,
            list: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            type: '', // 排序方式
            sort: '', // 升降序
            source: -1, // 渠道 null为全部 0taptap,1应用宝,2客户,3学习识别,4人工
            date: [] // 时间
        }
    },
    created () {
        // 初始化加载数据
        this.getList(1)
    },

    methods: {
        // 获取数据列表
        async getList (num) {
            if (this.loading) return
            this.loading = true
            this.pageNum = num

            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                sort: this.sort, // 升序还是降序
                type: this.type // 排序类型
            }

            // 选择日期
            if (this.date && this.date.length) {
                params.fromDate = this.date[0]
                params.toDate = this.date[1]
            }

            // 渠道
            if (this.source !== -1) {
                params.source = this.source
            }

            let res = await this.$http.get('/data/selectGameDataTotal', {
                params
            })

            if (res.code === '0') {
                this.list = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        // 去游戏详情
        goGameDetailBlank (id) {
            const { href } = this.$router.resolve({
                path: '/game/gameDetail',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        // 页容切换
        sizeChange (val) {
            this.pageSize = val
            this.getList(1)
        },

        // 页码切换
        pageChange (val) {
            this.getList(val)
        },

        // 初始化状态
        clearGetList () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1)
        },

        // 后端排序
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = ''
            } else {
                this.type = prop
            }
            this.getList(1)
        }
    },

    components: {
        gameAllDataExpand // 展开组建
    },

    filters: {
        timeInit (val) {
            if (val) {
                return formatDate(val, 'YYYY-MM-DD')
            } else {
                return '-'
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .game-all-data-box {
        padding-bottom: 10px;
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-left: 20px;
            }
        }
        .table-box {
            margin: 20px 0;
            /deep/ .el-table__expanded-cell {
                padding: 0;
                padding-bottom: 5px;
                padding-left: 109px;
                border-right: none;
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
